package top.suven.http.oauth.validator;

import top.suven.http.oauth.vo.OauthClientDetails;
import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.message.types.ResponseType;


import javax.servlet.http.HttpServletResponse;
import java.util.Set;


/**
 * 2018-08-01
 * <p/>
 *  *  客户端	客户详细资料 抽像模块类，用于公共验证和获取客户用户的信息；
 *  * 将通用的行为(方法) 位于此
 *
 * @author suven.wang
 */
public abstract class AbstractOAuthResponseTypeValidator extends AbstractOAuthTypeValidator {


//    protected OauthService oauthService = BeanProvider.getBean(OauthService.class);

    protected  OAuthAuthzRequest oauthRequest;

    protected OauthClientDetails clientDetails;

    protected AbstractOAuthResponseTypeValidator(OAuthAuthzRequest oauthRequest) {

        super(oauthRequest);
       this.oauthRequest = oauthRequest;
    }
    public String getState(){
        return oauthRequest.getState();
    }

    protected String responseType() {
        return oauthRequest.getResponseType();
    }
//    //2.检查客户端response_type是否正确
//    protected OAuthResponse validateOauthType() throws OAuthSystemException {
//        //1.校验客户端clientId是否正确  validate()
//        //2.检查客户端response_type是否正确
//        return validateResponseType();
//    }


    //3.//校验客户端validateGrantType是否正确
    protected final boolean validateResponseType() throws OAuthSystemException {
        final String responseType = this.responseType();
        if(responseType.equals(ResponseType.CODE.toString())
                || responseType.equals(ResponseType.TOKEN.toString())){
            return true;
        }
        logger.debug("Invalid response_type [{}] ", responseType);
        OAuthResponse response = invalidResponseTypeResponse(responseType);
        this.setResponse(response);
        return false;

    }

    public boolean validateScopes(Set<String> scopes, OauthClientDetails clientDetails) throws OAuthSystemException{
        final String clientDetailsScope = clientDetails.scope();          //read write
        if(null == clientDetailsScope || "".equals(clientDetailsScope)){
            OAuthResponse response = invalidScopeResponse();
            this.setResponse(response);
            return false;
        }
        for (String scope : scopes) {
            if (!clientDetailsScope.contains(scope)) {
                logger.debug("Invalid scope - ClientDetails scopes '{}' exclude '{}'", clientDetailsScope, scope);
                OAuthResponse response = invalidScopeResponse();
                this.setResponse(response);
                return false;
            }
        }
        return true;
    }






    /** Authorization接口返回对象 **/
    public OAuthResponse successResponse() throws OAuthSystemException {
        OAuthResponse.OAuthResponseBuilder builder = new OAuthResponse.OAuthResponseBuilder(HttpServletResponse.SC_OK);
        return builder.buildJSONMessage();

    }



}
